//
//  HomePageController.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/10/11.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit
import SwiftyJSON
import YYKit
import SDWebImage
import MJRefresh
class HomePageController: UIViewController ,UITableViewDelegate,UITableViewDataSource,imageViewTapDelegate,headViewDelegate{
    func tapIndex(index: Int) {
         print("我点击了",index)
    }
    
   
    func tapIndexBtn(index: Int, str: String) {
        print("我点击了\(str),我是第\(index)个")
        
        switch index {
        case 0:
            //精彩活动
            
            break
        case 1:
            //邀请有礼
            break
        case 2:
            //安全保障
            let actVC = ActiveADViewController()
            self.navigationController?.pushViewController(actVC, animated: true)
            break
        case 3:
            //新手指南
            break
        case 4:
            //签到
            break
        default:
            break
        }
        
        
    }
    
    
   
    

    private var HometTableView = UITableView()
    var dataArr = NSMutableArray()
    var scrollView = FZH_ScrollView()
   
    
    override func viewWillAppear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = false
       
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
//        title = "home"
        
//        self.automaticallyAdjustsScrollViewInsets = false
        HometTableView = UITableView.init(frame: CGRect(x: 0, y: -20, width: SCREEN_WIDTH, height: SCRENN_HEIGHT - kTabBarHight + 20 ))
//        self.view.backgroundColor = UIColor.colorWithHexString(color: "#EFEEF4")
        HometTableView.backgroundColor = UIColor.colorWithHexString(color: "#EFEEF4")
        if #available(iOS 11.0, *) {
            HometTableView.backgroundColor = UIColor.init(named: "MainBack", in: Bundle.main, compatibleWith: UITraitCollection.init())

        } else {
            // Fallback on earlier versions
            HometTableView.backgroundColor = UIColor.colorWithHexString(color: "#EFEEF4")
        }
        HometTableView.delegate = self
        HometTableView.dataSource = self;
        HometTableView.register(UINib(nibName: "HomeTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "HomeTableViewCell")
        //cell 去除线
//        HometTableView.separatorInset = UIEdgeInsets.zero
//        HometTableView.layoutMargins = UIEdgeInsets.zero;
        HometTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
        view.addSubview(HometTableView)
        HometTableView.tableFooterView = UIView.init()
        headView()
        InternetDataRequest()
    }
    //头试图
    func headView() -> Void {
        
        let backHeadView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 310 * scaleHeight))
        backHeadView.backgroundColor = UIColor.white
        //无线滚动图
        scrollView = FZH_ScrollView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 435 * 0.5 * scaleHeight))
        scrollView.delegate = self
//        scrollView.imageArr = [UIImage.init(named: "second.jpeg")!,UIImage.init(named: "third.jpeg")!,UIImage.init(named: "fourth.jpeg")!,UIImage.init(named: "fifth.jpeg")!]
        backHeadView.addSubview(scrollView)
        //anniutu
        let acView = ActivitiesHeadView.init(frame: CGRect(x: 0, y: 210 * scaleHeight, width: SCREEN_WIDTH, height: 95 * scaleHeight))
        acView.delegate = self
        
        backHeadView.addSubview(acView)
//        self.view.addSubview(backHeadView)
        HometTableView.tableHeaderView = backHeadView
        
        //刷新控件
        HometTableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.InternetDataRequest()
            self.HometTableView.mj_footer.isHidden = false
        })
        HometTableView.mj_header.ignoredScrollViewContentInsetTop = SCREEN_WIDTH * 3 / 8
        HometTableView.mj_header.isAutomaticallyChangeAlpha = true
       
        HometTableView.mj_header.beginRefreshing()
        
        
        HometTableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
//            print("footer refresh")
            sleep(2)
            self.HometTableView.mj_footer.isHidden = true
//            self.HometTableView.mj_footer.endRefreshing()
        })

    }
    
    
    func InternetDataRequest() -> Void {
        
//        HttpTool.shareTool.r
        let url = mainURL.appending(ProductHomePath)
        
        HttpTool.requestWithPost(url: url)
        HttpTool.shareTool.requestWithURL(URLString: url, MethodType: .post, parameters: [:], showHud: false, sucess: { (message, result) in
            
//
            let dict:NSDictionary = result as! NSDictionary
            
//            print("asdasd",dict .jsonPrettyStringEncoded() ?? "aa")
            
            let arrM:NSArray = dict.object(forKey: "Pic") as! NSArray
            
//            self.dataArr.addObjects(from: arrM as! [Any])
            
            
//            print(self.dataArr)
            let imageArr = NSMutableArray.init()
            
            for indexM in arrM {
//                let bannerModel = BannerImageModel.json
                let model = BannerImageModel.init()
                
                let dict:NSDictionary = indexM as! NSDictionary
                
                model.PicUrl = dict.object(forKey: "PicUrl") as! String
                
                self.dataArr.add(model)
                
                let urlStr = "\(staticURL)".appending(model.PicUrl);
                imageArr.add(urlStr)
                
            }
            self.scrollView.imageArr = imageArr as Array<AnyObject>
            
//            let bannerModel = BannerImageModel.model(withJSON: <#T##Any#>)
            self.HometTableView.mj_header.endRefreshing()
            
        }, fail: { (failMessage) in
            print(failMessage ?? "")
        }) { (error) in
            print(error?.localizedDescription ?? " a")
        }
        
    }
    
    
    
    //tableViewdelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        if indexPath.row == 0 {
//            return 97 * scaleHeight
//        }
        return 116 * scaleHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "HomeTableViewCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId) as? HomeTableViewCell
       
//        cell?.textLabel?.text = "cell"
        cell?.selectionStyle = UITableViewCellSelectionStyle.none
//        print(indexPath.row)
        cell!.commit()
        return cell!
        
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        cell.responds(to: #selector())
//        cell.separatorInset = UIEdgeInsets.zero
//        cell.layoutMargins = UIEdgeInsets.zero
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
